package com.iaiai.imail.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.iaiai.imail.core.IActivity;
import com.iaiai.imail.helper.MessageBox;

/**
 * 
 * <p>
 * Title: CreateInfoActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-9-12 下午2:25:55
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class CreateInfoActivity extends IActivity {

	private Button type;
	private Button passwordSave; // 是否记住密码

	private EditText password, info; // 密码跟描述

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.create_info);
		
		setTitleLayout(); // 设置头

		setTitleText(getResources().getString(R.string.create_email_title));

		///////////////////////类型
		type = (Button) findViewById(R.id.type);
		if (getBundle().getString("type") != null
				&& getBundle().getString("type").equals(getResources().getString(R.string.pop3))) {
			type.setText(getResources().getString(R.string.pop3));
		} else {
			type.setText(getResources().getString(R.string.pop3));
		}
		type.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MessageBox.showToast(CreateInfoActivity.this, getResources().getString(R.string.msg_create_type));
			}
		});
		////////////////////是否保存密码
		passwordSave = (Button) findViewById(R.id.password_save);
		if (getBundle().getString("password_save") != null) {
			if (getBundle().getString("password_save").equals(getResources().getString(R.string.password_save))) {
				passwordSave.setText(getResources().getString(R.string.password_save));
			} else {
				passwordSave.setText(getResources().getString(R.string.password_no));
			}
		} else {
			passwordSave.setText(getResources().getString(R.string.password_save));
		}
		passwordSave.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (passwordSave.getText().toString().trim().equals(getResources().getString(R.string.password_save))) {
					passwordSave.setText(getResources().getString(R.string.password_no));
				} else {
					passwordSave.setText(getResources().getString(R.string.password_save));
				}
			}
		});
		///////////////////上一步
		findViewById(R.id.pre).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent activity = new Intent(CreateInfoActivity.this, CreateActivity.class);
				activity.putExtras(getAttribute());
				startActivity(activity);
				finish(); // 关闭当前
			}
		});
		/////////////////下一步
		findViewById(R.id.next).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(password.getText().toString().trim().length()<=0){
					password.setError(getResources().getString(R.string.msg_create_password_null));
					return;
				}
				
				Intent activity = new Intent(CreateInfoActivity.this, CreateTestActivity.class);
				activity.putExtras(getAttribute());
				startActivity(activity);
				finish(); // 关闭当前
			}
		});
		/////////////////密码
		password = (EditText) findViewById(R.id.password);
		////////////////描述
		info = (EditText) findViewById(R.id.info);
		info.setText(getBundle().getString("email").substring(0,getBundle().getString("email").indexOf("@")));
	}
	
	/**
	 * 设置头
	 */
	private void setTitleLayout() {
		if (getBundle().getBoolean("create")) {
			// 显示返回按钮
			insertTitleLayoutButton(R.drawable.icon_return, new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent activity = new Intent(CreateInfoActivity.this, MainActivity.class);
					startActivity(activity);
					finish();
					overridePendingTransition(R.anim.push_down_out, R.anim.push_down_in);
				}
			});
		}
	}

	/**
	 * 存储数据
	 * 
	 * @return
	 */
	private Bundle getAttribute() {
		Bundle bundle = getBundle();
		bundle.putString("password_save", passwordSave.getText().toString().trim());
		bundle.putString("password", password.getText().toString().trim());
		bundle.putString("type", type.getText().toString().trim());
		bundle.putString("info", info.getText().toString().trim());
		return bundle;
	}

}
